<?php
/**
 * 《快递安排》实现类
 *
 * @author 孙晓晔
 * @version $Id: Car.class.php 1033 2010-03-25 11:26:55Z sunxy $
 */
require_once(dirname(__FILE__) . '/ExpressUtil.class.php');

class Plan extends ExpressUtil {
	public $tbl = Constant::tbl_express_plan;

	public $payment = array(
		// '-' => '请选择',
		'预付50%' => '预付50%',

		'预付全款' => '预付全款',	
		'客户到付' => '客户到付',
	
		'当月结算' => '当月结算',
		'月结30天' => '月结30天',
		
		'其它' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待走货',
		'2' => '正在走货',
		// '3' => '已经走货',
		'3' => '完成走货',

		'5' => '暂停走货',
		'6' => '取消走货',
		
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'supplier' => '供应商',
		'number' => '运单号码',
		'ets' => '送货日期',
		'etd' => '离港日期',
		'eta' => '到港日期',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'supplier' => '供应商',
		'number' => '运单号码',
	
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('payment_option', $this->payment);
	}
	
	function mobile($controller, $request) {
		$this->pretreat($controller);
	
		global $CategoryTree;
	
		$data = $CategoryTree['货运管理'];
	
		$ret = array();
		foreach ($data as $k => $v) {
			if ($this->tree_my($v)) {
				$ret[$k] = $v;
			}
		}
	
		$controller->assign('menu', $ret);
	
		$controller->display($request, 'mobile');
		return true;
	}
	
	function tab($controller, $request) {		
		$data = array(
			'货运需求' => array('cc'=>'express.php?do=Requirement.find', 'js'=>'1'),
			'货运安排' => array('cc'=>'express.php?do=Plan.find', 'js'=>'1'),
			'报关信息' => array('cc'=>'express.php?do=Export.find', 'js'=>''),
			'报关明细' => array('cc'=>'express.php?do=ExportItem.find', 'js'=>''),

			// '装箱单' => array('cc'=>'express.php?do=Packing.find', 'js'=>''),
			'商业发票' => array('cc'=>'express.php?do=Invoice.find', 'js'=>''),
			'支出信息' => array('cc'=>'finance.php?do=OutMy.find', 'js'=>''),
		
			'进口业务' => array('cc'=>'express.php?do=In.find', 'js'=>''),
			// '货运公司' => array('cc'=>'contact.php?do=Express.find', 'js'=>''),
		
			'统计分析' => array('cc'=>'express.php?do=Plan.tj_tab', 'js'=>''),
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tab');
		return true;
	}
	
	function tj_tab($controller, $request) {		
		$data = array(
			'货运需求' => array('cc'=>'express.php?do=Requirement.group', 'js'=>''),
			'货运安排' => array('cc'=>'express.php?do=Plan.group', 'js'=>''),		
			'进口业务' => array('cc'=>'express.php?do=In.group', 'js'=>''),
			'报关信息' => array('cc'=>'express.php?do=Export.group', 'js'=>''),				
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);

		$controller->display($request, 'tj_tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 year'));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '货运员',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'express/plan/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == 'ets') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['ets'];
		}
		
		if ($px == 'etd') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['etd'];
		}
		
		if ($px == 'eta') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['eta'];
		}
		
		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$supplier_s = $this->_input($request['supplier_s']);
		$number_s = $this->_input($request['number_s']);
		
		$emp_t_s = $request['emp_t_s'];
		if ($emp_t_s == 'emp') {
			$request['emp_s'] = $request['emp_a_s'];
		} else if ($emp_t_s == 'oper') {
			$request['oper_s'] = $request['emp_a_s'];
		}

		$where = " WHERE 1=1";

		if (!empty($number_s)) {
			$where .= " AND number = '" . $number_s . "'";
		}

		$where .= $this->_where_x("way", $request['way_s']);
		$where .= $this->_where_x("payment", $request['payment_s']);
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row['num'] = $this->_num($row['id']);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'express/plan/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('1 day'));

		$controller->display($request, 'express/plan/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$way = $request['way'];
		$payment = addslashes($request['payment']);
		
		$cost = $request['cost'];
		$currency = $request['currency'];
		
		$dst = $request['dst'];
		$voyage = $request['voyage'];
		$number = $request['number'];
		$number = str_replace(" ", "", $number);
		
		$ets = $request['ets'];
		$etc = $request['etc'];
		$etd = $request['etd'];
		$eta = $request['eta'];
		$date = $request['date'];

		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(supplier, supplier_x, payment, way, cost, currency, dst, voyage, number, ets, etc, etd, eta, date, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $supplier, $supplier_x, $payment, $way, $cost, $currency, $dst, $voyage, $number, $ets, $etc, $etd, $eta, $date, $content, $emp, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function freightPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$way = $request['way'];
		$content = addslashes($request['content']);
		
		// $emp = $this->_input($request['emp']);
		// $office = $this->_office($emp);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(supplier, supplier_x, way, content, emp, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $supplier, $supplier_x, $way, $content, $_SESSION['id'], $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
		$plan = $db->insert_id();

		$content = addslashes($request['content']);

		$fmt = "UPDATE %s SET plan = '%s', content = concat(content, '%s'), status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_express_requirement, $plan, $content, $id);
		// debug($sql);
		$db->query($sql);

		// $this->_notice_2($id, $plan);

		return true;
	}

	function _notice_2($id, $plan) {
		$requirement = $this->id(Constant::tbl_car_requirement, $id);

		$row = $this->_view($plan);

		$subject = "[通知]已经派车";
		$body = sprintf("车辆：%s\n司机：%s\n路线：%s\n时间：%s\n\n%s\n%s\n", $row['car_t'], $row['driver_t'], $row['address'], $row['date'], Name::emp($_SESSION['id']), $row['createtime']);

		$this->_notice($_SESSION['id'], $requirement['emp'], $subject, $body);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'express/plan/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$way = $request['way'];
		$payment = addslashes($request['payment']);
		
		$cost = $request['cost'];
		$currency = $request['currency'];
		
		$dst = $request['dst'];
		$voyage = $request['voyage'];
		$number = $request['number'];
		$number = str_replace(" ", "", $number);
		
		$ets = $request['ets'];
		$etc = $request['etc'];
		$etd = $request['etd'];
		$eta = $request['eta'];
		$date = $request['date'];

		$content = addslashes($request['content']);
		
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET supplier = '%s', supplier_x = '%s', payment = '%s', way = '%s', cost = '%s', currency = '%s', dst = '%s', voyage = '%s', ";
		$fmt .= "number = '%s', ets = '%s', etc = '%s', etd = '%s', eta = '%s', date = '%s', content = '%s', emp = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $supplier, $supplier_x, $payment, $way, $cost, $currency, $dst, $voyage, $number, $ets, $etc, $etd, $eta, $date, $content, $emp, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'express/plan/copy');
		return true;
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		
		$this->_files_clean($id);
		
		$row = $this->_view($id);
	
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'express/plan/upload');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function finish($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'express/plan/finish');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function finishPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);

		$eta = $request['eta'];
		$date = $request['date'];

		$content = sprintf("%s\n%s 操作", addslashes($request['content']), Name::emp($_SESSION['id']));

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET eta = '%s', date = '%s', content = '%s', status = '3', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $eta, $date, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
		
		$fmt = "UPDATE %s SET status = '4' WHERE plan = '%s'";
		$sql = sprintf($fmt, Constant::tbl_express_requirement, $id);
		debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function out($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		require_once(dirname(__FILE__) . '/Requirement.class.php');
		$requirement = new Requirement();		
		$list = $requirement->_plan_requirement($row['id']);
		
		$name = '';
		$a = array();
		while (list($k, $v) = each($list)) {
			$a[] = $v['mold_t'];
		}
		
		$row['name'] = implode("/", $a) . $row['way_t'];
		$row['amount'] = $row['cost'];				
		$row['type'] = '4';

		$controller->assign_by_ref('result', $row);
		
		require_once(dirname(__FILE__) . '/../finance/OutMy.class.php');
		$aa = new OutMy();
		
		$aa->add($controller, $request);
	}
	
	function outPost($controller, $request) {
		require_once(dirname(__FILE__) . '/../finance/OutMy.class.php');
		$aa = new OutMy();
		
		$out = $aa->_add($controller, $request);
		
		$id = $request['id'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET `out` = '%s' WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $out, $id);
		debug($sql);
		$db->query($sql);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);

		$controller->assign_by_ref('result', $row);

		require_once(dirname(__FILE__) . '/Requirement.class.php');
		$requirement = new Requirement();		
		$controller->assign_by_ref('requirement', $requirement->_plan_requirement($row['id']));

		$controller->display($request, 'express/plan/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}

		$row['supplier_x'] = json_decode($row['supplier_x']);
		
		// $row['number'] = $this->_format($row['number'], 4, ' ');
		
		$row['way_t'] = $this->way[$row['way']];
		
		$row = $this->_view_t($row);
		
		return $row;
	}
	
	function track($controller, $request) {
		$supplier = $request['supplier'];
		
		if ($express == '1219') {
			return $this->ups($controller, $request);
		} else if ($supplier == '1125' || $supplier == '1218') {
			return $this->fedex($controller, $request);
		} 
		
		return false;
	}
	
	function ups($controller, $request) {
		require_once(dirname(__FILE__) . '/../api/ups/UpsAPI.php');
		
		$tracking_number = $request['number'];
		
		$tracking = new UpsAPI_Tracking($tracking_number);

		$xml = $tracking->buildRequest();
		$response = $tracking->sendRequest($xml, true);		
		$unserializer = new XML_Unserializer(array('returnResult' => true));
		$data = $unserializer->unserialize($response);
		
		$controller->assign_by_ref('tt', print_r($data, true));
		
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'express/plan/ups');
		return true;
	}
	
	function fedex($controller, $request) {
		require_once(dirname(__FILE__) . '/../api/fedex/fedexdc.php');
		
		$a = preg_split("/[\s,]/i", $request['number']);
		debug(print_r($a, true));
		
		$s = '';
		for ($i = 0; $i < count($a); $i++) {
			$s .= $a[$i];
		}
		$tracking_number = $s;
		
		
		// $tracking_number = implode(",", $a);
		
		// $tracking_number = str_replace(" ", "", $request['number']);
		debug($tracking_number);
		$fed = new FedExDC('510087941','100036862');

		//tracking example
		$data = $fed->ref_track(
		    array(
		        '1537' => $tracking_number,
		        '1534' =>'Y'
		    )
		);
		
		$count = intval($data['1715-1']);
		$ret = array();
		for ($k = 1; $k <= $count; $k++) {
			$act['code'] = $data['1171-1-'.$k];
			$act['action'] = $data['1159-1-'.$k];
			$act['city'] = $data['1160-1-'.$k];
			$act['country'] = $data['1164-1-'.$k];
			$act['date'] = $data['1162-1-'.$k];
			$act['time'] = $data['1163-1-'.$k];
			$ret[] = $act;
		}
		
		$data['Activity'] = $ret;
		
		$data['code'] = $data['29-1'];
		$data['pickupdate'] = $data['24-1'];
		$data['deliver_to'] = $data['1705-1'];
		$data['signed_for'] = $data['1706-1'];
		
		$data['total_package_weight'] = $data['1401-1'];
		$data['weight_units'] = $data['75-1'];
		
		$data['package_type_description'] = $data['1718-1'];
				
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'express/plan/fedex');
		return true;
	}

	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$row = $this->id($this->tbl, $id);
			
			if ($this->_is_delete($row)) {
				$fmt = "DELETE FROM %s WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $id);
				// debug($sql);
				$db->query($sql);				
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($row['status'] > 1) {
			return false;
		}

		if ($row['emp'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function _driver($emp) {
		$row = $this->id(Constant::tbl_emp, $emp);
		$ret = $row['name'];

		if (!empty($row['mobile']) && !empty($row['short'])) {
			return sprintf("%s(%s/%s)", $row['name'], $row['mobile'], $row['short']);
		}

		return sprintf("%s(%s)", $row['name'], $row['mobile']);
	}

	function _num($id) {
		require_once('Requirement.class.php');
		$requirement = new Requirement();
		$ret = $requirement->_plan_requirement($id);
		if (is_array($ret)) {
			return count($ret);
		}

		return 0;
	}

	

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		// $controller->assign('t2', strtotime('+1 day'));

		$num = array(
			'1' => '按次数',
			'2' => '按里程',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'express/plan/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t1, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(t1, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(t1, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(km) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(km) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp' || $px == 'driver') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_code($controller, $request) {

		$data = $this->_ajax_code($request);
		echo json_encode($data);
	}

	function _ajax_code($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND code LIKE '%s'";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$ret[] = $row;
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reason($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		require_once('Requirement.class.php');
		$requirement = new Requirement();		
		$controller->assign_by_ref('requirement', $requirement->_plan_requirement($row['id']));

		$controller->display($request, 'express/plan/reason');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function reasonPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$reason = addslashes($request['reason']);

		$status = $request['status'];

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $reason, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$fmt = "UPDATE %s SET reason = '%s', status = '%s', modifytime = now() WHERE status = '3' AND plan = '%s'";
		$sql = sprintf($fmt, Constant::tbl_express_requirement, $reason, $status, $id);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function status_continue($controller, $request) {

		return $this->status_restart($controller, $request);
	}

	function status_restart($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, '1', $id);
		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 定时运行，自动更新等待出发的用车安排
	 */
	function status_update($controller, $request) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND createtime > DATE_ADD(now(), INTERVAL -1 DAY)";
		// $fmt = "SELECT * FROM %s WHERE status = '1'";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			if (strtotime($row['t1']) > time()) {
				echo "S";
				continue;
			}

			if ($this->_num($row['id']) == 0) {
				continue;
			}

			echo ".";

			$fmt = "UPDATE %s SET status = '2' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}

	function tj_daily($controller, $request) {
		$this->pretreat($controller);

		$request['tpl'] = "html4";

		$from = $this->email;
		$to = 'sunxy@tecmold.com';

		$subject = '司机出勤申情况';

		$this->result_s = 20;
		$this->groupby_s = 'driver';

		$body = $this->_tj_a($controller, $request);

		$this->mail_html($from, $to, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function u($controller, $request) {
		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$code = $this->u_code(strtotime($row['createtime']));

			$fmt = "UPDATE %s SET code = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $code, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function u_code($time) {
		$ret = '';
		$dd = sprintf("%s", date('yW00', $time));

		do {
			$dd++;
			$dd = sprintf("%06d", $dd);
			$ret = "E" . $dd;
		} while ($this->_code_exist($ret));

		return $ret;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' ORDER BY id DESC LIMIT 50";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$ret[] = $row;
		}

		return $ret;
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE supplier_x is NULL";
		$fmt = "SELECT * FROM %s WHERE length(supplier_x) < 10";
		$fmt = "SELECT * FROM %s WHERE createtime < '2011-05-09'";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$supplier_x = $this->_supplier_x($row['supplier']);			
			
			$fmt = "UPDATE %s SET supplier_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $supplier_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function x($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s ORDER BY id DESC";
		// $fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$j = json_decode($row['supplier_x']);
			
			if (!empty($j->contact)) {
				continue;
			}
		
			$supplier_x = $this->_x($row['supplier'], $row['supplier_x']);
			
			$fmt = "UPDATE %s SET supplier_x = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $supplier_x, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _x($id, $supplier_x) {
		$j = json_decode($row['supplier_x']);
		// var_dump($j);
		
		require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');
		$aa = new Supplier();
		
		$c = $aa->_view($id);
				
		$ret['name'] = $c['name'];
		$ret['fullname'] = $c['fullname'];
		$ret['address'] = $c['address'];
		$ret['web'] = $c['url'];
		
		if (empty($j->contact)) {
			$ret['contact'] = $c['c']['name'];
		} else {
			$ret['contact'] = $j->contact;
		}
		
		$ret['mobile'] = $c['c']['mobile'];
		$ret['tel'] = $c['c']['tel'];
		$ret['fax'] = $c['c']['fax'];
		$ret['email'] = $c['c']['email'];		
		$ret['content'] = $c['c']['content'];
		
		$ret = json_encode($ret);
		
		return addslashes($ret);
	}
	
	function _supplier($supplier) {
		$dd = date('Y-m-d', strtotime("-18 MONTH"));
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE supplier = '%s' AND createtime > '%s'";
		$sql = sprintf($fmt, $this->tbl, $supplier, $dd);
		echo $sql . "\n";
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		if ($row['t'] > 0) {
			return true;
		}
	
		return false;
	}
	
	function view_supplier($controller, $request) {
		$id = $request['id'];
	
		$ret = $this->_list_supplier($id);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'express/plan/_list_c');
		return true;
	}
}
?>
